package com.sm.ui;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sm.domain.Score;
import com.sm.service.IScoreService;
import com.sm.util.ExcelUtil;
import com.sm.util.JTableUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.*;

@Component
public class ReportUI implements BaseUI {

    @Autowired
    private IScoreService service;
    private JPanel panel;
    private JScrollPane scrollPane;
    private JTable table;
    private JTabbedPane tablePane;

    @Override
    public void init(JTabbedPane tablePane){
        this.tablePane = tablePane;
        panel = new JPanel();
        createSelect();
        createList();
        createBtn();
    }

    private void createSelect(){
        JPanel jb = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel lNo = new JLabel("按课程和学期统计成绩分布结果如下：");
        lNo.setFont(new java.awt.Font("幼圆", 1, 24));
        jb.add(lNo);
        panel.add(jb);
    }

    private void createBtn(){
        JPanel jb = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton btnAdd = new JButton("导出Excel");
        jb.add(btnAdd);
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                QueryWrapper<Score> qw = new QueryWrapper<>();
                List<Map<String, Object>> list = service.listTotal(qw);
                //导出 简单excel表 到指定路径
                String filePath3 = "成绩统计分析.xlsx";
                List<List<Object>> data = new ArrayList<>();
                for (Map<String, Object> map : list) {
                    List v = new ArrayList();
                    v.add(map.get("courseName"));
                    v.add(map.get("xq"));
                    v.add(map.get("maxS"));
                    v.add(map.get("minS"));
                    v.add(map.get("avgS"));
                    v.add(map.get("y"));
                    v.add(map.get("l"));
                    v.add(map.get("z"));
                    v.add(map.get("c"));
                    data.add(v);
                }
                List<String> head = Arrays.asList("课程", "学期", "最高分", "最低分", "平均分", "优(人)", "良(人)", "中(人)", "差(人)");
                ExcelUtil.writeBySimple(filePath3, data, head);
                JOptionPane.showMessageDialog(null, "导出成功！", "成功提示", JOptionPane.PLAIN_MESSAGE);
            }
        });
        panel.add(jb);
    }


    private void createList(){
        tablePane.addTab("统计分析", panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        String[] columnNames = {"课程", "学期", "最高分", "最低分", "平均分", "优(人)", "良(人)", "中(人)", "差(人)"};
        DefaultTableModel dataModel = new DefaultTableModel();
        dataModel.setColumnIdentifiers(columnNames);
        select(dataModel, null);
        table = new JTable(dataModel) {
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };
        JTableUtil.setTableColumnCenter(table);
        JTableUtil.setbgcolor(table);
        // 通过JTable对象创建JScrollPane，显示数据
        scrollPane = new JScrollPane(table);
        panel.add(scrollPane);
    }

    private void select(DefaultTableModel dataModel, Score vo){
        dataModel.setRowCount(0);
        QueryWrapper<Score> qw = getQueryWrapper(vo);
        List<Map<String, Object>> list = service.listTotal(qw);
        for (Map<String, Object> map : list) {
            Vector v = new Vector();
            v.add(map.get("courseName"));
            v.add(map.get("xq"));
            v.add(map.get("maxS"));
            v.add(map.get("minS"));
            v.add(map.get("avgS"));
            v.add(map.get("y"));
            v.add(map.get("l"));
            v.add(map.get("z"));
            v.add(map.get("c"));
            dataModel.addRow(v);
        }
    }

    private QueryWrapper<Score> getQueryWrapper(Score vo){
        QueryWrapper<Score> qw = new QueryWrapper();
        if (vo == null) {
            return qw;
        }
        if (StringUtils.isNotEmpty(vo.getStuNo())) {
            qw.eq("s.stuNo", vo.getStuNo());
        }
        return qw;
    }


}

